package com.qingmu.club.minaapi.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

/**
 * Calendar操作Date
 */
public class DateUtil {

    /**设置初始值*/
    private static Calendar cal;

    /**
     * 根据当前系统时区定义Calendar
     */
    static {
        cal = Calendar.getInstance();
    }

    /**
     * Date-->String 转换
     * @param date
     * @return
     */
    public static String dateToString(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * 获取指定日期在一周中的第几天
     * @param date
     */
    public static void getDayOfWeek(Date date){
        int dayOfWeek = cal.DATE;
        System.out.println(dayOfWeek);
    }

    /**
     * 获取指定日期是月中的第几天
     * @param date
     */
    public static void getDayOfMonth(Date date){
        int day = date.getDate();
        System.out.println(day);
    }

    /**
     * 获取指定时间所在月份
     * @param date
     */
    public static void getMonth(Date date){
        cal.setTime(date);
        //老外计算月份从0开始，转换为国内月份需要加1
        int month = cal.get(cal.MONTH) + 1;
        System.out.println(month);
    }

    /**
     * 获取指定时间所在周的周一（一天中最小时间）和周天（一天中最大时间）
     * @param date
     */
    public static HashMap<String,Object> getFristAndLashOfWeekDay(Date date){
        cal.setTime(date);
        cal.set(cal.DAY_OF_WEEK,cal.MONDAY);
        Date mondayOld = cal.getTime();
        //老外的一周从周天开始，使用cal.SUNDAY获取到的是上周的周天
        cal.set(cal.DAY_OF_WEEK,cal.SATURDAY);
        //获取周六，加上一天的毫秒数，计算所在周周天，
        cal.setTimeInMillis(cal.getTimeInMillis()+24*60*60*1000);
        Date sundayOld = cal.getTime();

        //考虑到使用判断获取数据时存在部分数据不显示问题，逐将日期格式化处理
        Date mondayNew = mondayOld;
        Date sundayNew = sundayOld;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
            mondayNew = sdf.parse(sdf.format(mondayOld));
            cal.setTime(sdf.parse(sdf.format(sundayOld)));
            //用日期获取数据时会由1秒误差，若要求比较严格，建议不减1，获取下一天的开始时间
            cal.setTimeInMillis(cal.getTimeInMillis()+24*60*60*1000-1);
            sundayNew = cal.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            System.out.println("执行类：" + DateUtil.class + "中方法：getStartAndEndWeekDay(Date date) 异常");
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("startDate",simpleDateFormat.format(mondayNew));
        hashMap.put("endDate",simpleDateFormat.format(sundayNew));
        return hashMap ;
    }


    /**
     * 获取指定时间所在季度的第一天（一天中最小时间）和最后一天（一天中最大时间）
     * @param date
     */
    public static HashMap<String,Object> getFristAndLashOfDuarterDay(Date date){
        String str= new SimpleDateFormat("yyyy").format(date);
        cal.setTime(date);
        int month = cal.get(cal.MONTH) + 1;
        int quarter = 0;
        //判断季度
        if (month >= 1 && month <= 3) {
            quarter = 1;
        } else if (month >= 4 && month <= 6) {
            quarter = 2;
        } else if (month >= 7 && month <= 9) {
            quarter = 3;
        } else {
            quarter = 4;
        }
        //获取指定时间所在季度第一天和最后一天
        Date firstDay = null;
        Date lastDay = null;
        //String str = String.valueOf(date.getYear());
        String startDay = "";
        String endDay = "";
        switch (quarter){
            case 1://第一季度
                startDay = str + "-01-01 00:00:00";
                endDay = str + "-03-31 23:59:59";
                break;
            case 2://第二季度
                startDay = str + "-04-01 00:00:00";
                endDay = str + "-06-30 23:59:59";
                break;
            case 3://第三季度
                startDay = str + "-07-01 00:00:00";
                endDay = str + "-09-30 23:59:59";
                break;
            case 4://第四季度
                startDay = str + "-10-01 00:00:00";
                endDay = str + "-12-31 23:59:59";
                break;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            firstDay = sdf.parse(startDay);
            //用日期获取数据时会由1秒误差，若要求比较严格，建议不减1，获取下一天的开始时间
            cal.setTime(sdf.parse(endDay));
            cal.setTimeInMillis(cal.getTimeInMillis()+24*60*60*1000-1);
            lastDay = cal.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("startDate",dateToString(firstDay));
        hashMap.put("endDate",dateToString(lastDay));
        return hashMap ;
    }

}